package com.xpxrule.dataobject.biz.model;

import lombok.Data;

import java.io.Serializable;

/**
 * @version 1.0
 * @Author xiangpengxiang
 * @Date 2024/11/21 14:42
 * @注释
 */
@Data
public class ReturnResponse<T> implements Serializable {
    public static final int SUCCESS_CODE = 200;
    public static final int FAIL_CODE = 500;

    public static final ReturnResponse<String> SUCCESS = new ReturnResponse<String>(null);
    public static final ReturnResponse<String> FAIL = new ReturnResponse<String>(FAIL_CODE, null);

    private int code;
    private String msg;
    private T content;

    public ReturnResponse(){}
    public ReturnResponse(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public ReturnResponse(T content) {
        this.code = SUCCESS_CODE;
        this.content = content;
        this.msg = "success";
    }

    public static ReturnResponse success(Object content) {
        ReturnResponse<Object> response = new ReturnResponse<>();
        response.setCode(SUCCESS_CODE);
        response.setMsg("success");
        return response;
    }

    public static ReturnResponse success(Object content, String msg) {
        ReturnResponse response = success(content);
        response.setMsg(msg);
        return response;
    }

    public static ReturnResponse fail(String msg) {
        ReturnResponse response = new ReturnResponse();
        response.setCode(FAIL_CODE);
        response.setMsg(msg);
        return response;
    }

    @Override
    public String toString() {
        return String.format("ReturnResponse{code=%d, msg='%s', content=%s}", code, msg, content);
    }
}
